package com.atech.ebayadmin.common.tablibrary;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.lang.StringUtils;

public class EbayTab {
	public static int getPositiveIntValue(String intStr, int defaultInt) {
		try {
			int value = Integer.parseInt(intStr);
			if (value <= 0) {
				return defaultInt;
			}
			return value;
		} catch (Exception e) {
			return defaultInt;
		}
	}

	public static int getIntValue(String intStr, int defaultInt) {
		try {
			return Integer.parseInt(intStr);
		} catch (Exception e) {
			return defaultInt;
		}
	}

	public static Integer getIntegerValue(String intStr, Integer defaultInt) {
		try {
			return Integer.parseInt(intStr);
		} catch (Exception e) {
			return defaultInt;
		}
	}

	public static String XMLEncode(String txt) {
		txt = txt.replaceAll("[&]", "&amp;");
		txt = txt.replaceAll("[<]", "&lt;");
		txt = txt.replaceAll("[>]", "&gt;");
		txt = txt.replaceAll("[\"]", "&#034;");
		txt = txt.replaceAll("[']", "&#039;");
		return txt;
	}
	
	public static String enterToBR(String str) {
    	str = XMLEncode(str);
        str = str.replaceAll("  ", "&#160;&#160;");
        str = str.replaceAll("\r\n", "\n");
        str = str.replaceAll("\n", "<br/>");
        str = str.replaceAll("\t", "&#160;&#160;&#160;&#160;");
        return str;
    }
	
	public static String getAbbr(String source, String ab, int replaceShowCount, int finalLength) {
		if (StringUtils.isEmpty(source)) {
			return source;
		}
		if (source.length() <= finalLength) {
			return source;
		}
		if (ab == null) {
			ab = ".";
		}
		StringBuilder sb = new StringBuilder();
		int length = ab.length() * replaceShowCount;
		int position = finalLength;
		if (finalLength - length < finalLength) {
			position = finalLength - length;
		}
		if (position < 0) {
			while (position < 0 && replaceShowCount > 0) {
				position += ab.length();
				replaceShowCount--;
			}
		}
		//֤��numһ��ʼ��Ϊ<0
		if (position < 0) {
			return source;
		}
		sb.append(source.substring(0, position));

		for (int i = 0; i < replaceShowCount; i++) {
			sb.append(ab);
		}
		return sb.toString();
	}
	
	public static String getAbbr(String source, int finalLength) {
		return getAbbr(source, ".", 3, finalLength);
	}
	
	public static String encodeURIComponent(String str, String encoding){
		if(StringUtils.isNotBlank(str)){
			try {
				return URLEncoder.encode(str,encoding);
			} catch (UnsupportedEncodingException e) {
				return str;
			}
		}
		return "";
	}

}
